package net.atomarrow.render;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.atomarrow.ioc.ActionContext;

public class JsonRender extends Render {
    public static final String JSON_DATA="JSON_DATA";
    public static final String JSON_STRING="JSON_STRING";

	@Override
	public void render() {
	    jsonPrintData(getAttr(JSON_STRING));
	}

	   /**
     * json返回数据
     * 
     * @
     */
    private void jsonPrintData(String data){
        if(data==null){
            return;
        }
        PrintWriter out = getResponseWriter();
        out.print(data);
        out.flush();
        out.close();
    }
    /**
     * 得到Response的Writer
     * 
     * @return
     * @
     */
    protected PrintWriter getResponseWriter() {
        HttpServletResponse response = ActionContext.getContext().getResponse();
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        try {
            return response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
